package tom.client.invoice;

import com.google.gwt.cell.client.TextInputCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SingleSelectionModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import tom.client.DataForm;
import tom.client.Library;
import tom.shared.ContDept;
import tom.shared.Contact;
import tom.shared.InvItem;
import tom.shared.Stock;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;

public class FormAddrDept extends DataForm {
	private static FormAddrDept panel;

	public static FormAddrDept getPanel() {
		if (panel == null)
			panel = GWT.create(FormAddrDept.class);
		return panel;
	}

	private final List<ContDept> addrDeptList;
	private final ListDataProvider<ContDept> itemData = new ListDataProvider<ContDept>();
	private final CellTable<ContDept> cellTable;
	private final SingleSelectionModel<ContDept> itemSelection = new SingleSelectionModel<ContDept>();

	public FormAddrDept() {
		setSize("880px", "500px");
		ScrollPanel scrollPanel = new ScrollPanel();
		add(scrollPanel, 23, 141);
		scrollPanel.setSize("850px", "180px");

		cellTable = new CellTable<ContDept>();
		cellTable.setStyleName("table");
		scrollPanel.setWidget(cellTable);
		cellTable.setPageSize(10);
		cellTable.setTableLayoutFixed(true);
		cellTable.setAccessKey('x');
		cellTable.setSize("100%", "100%");
		itemData.addDataDisplay(cellTable);

		Column<ContDept, String> colPrefix = new Column<ContDept, String>(
				new TextInputCell()) {
			@Override
			public String getValue(ContDept object) {
				return object.prefix;
			}
		};

		colPrefix.setCellStyleNames("table");
		cellTable.addColumn(colPrefix, "คำนำ");
		cellTable.setColumnWidth(colPrefix, "60px");

		Column<ContDept, String> colName = new Column<ContDept, String>(
				new TextInputCell()) {
			@Override
			public String getValue(ContDept object) {
				return object.name;
			}
		};
		colName.setCellStyleNames("table");
		cellTable.addColumn(colName, "ชื่อ");
		cellTable.setColumnWidth(colName, "150px");

		Column<ContDept, String> colSurname = new Column<ContDept, String>(
				new TextInputCell()) {
			@Override
			public String getValue(ContDept object) {
				return object.surname;
			}
		};
		colSurname.setCellStyleNames("table");
		cellTable.addColumn(colSurname, "นามสกุล");
		cellTable.setColumnWidth(colSurname, "150px");

		Column<ContDept, String> colDept = new Column<ContDept, String>(
				new TextInputCell()) {
			@Override
			public String getValue(ContDept object) {
				return object.dept;
			}
		};
		colDept.setCellStyleNames("table");
		cellTable.addColumn(colDept, "ฝ่าย");
		cellTable.setColumnWidth(colDept, "150px");

		Column<ContDept, String> colPhone = new Column<ContDept, String>(
				new TextInputCell()) {
			@Override
			public String getValue(ContDept object) {
				return object.phone;
			}
		};
		colPhone.setCellStyleNames("table");
		cellTable.addColumn(colPhone, "เบอร์ติดต่อ");
		cellTable.setColumnWidth(colPhone, "150px");
		
		Column<ContDept, String> colEmail = new Column<ContDept, String>(
				new TextInputCell()) {
			@Override
			public String getValue(ContDept object) {
				return object.email;
			}
		};
		colEmail.setCellStyleNames("table");
		cellTable.addColumn(colEmail, "Email");
		cellTable.setColumnWidth(colEmail, "150px");
		
		Button btnInsert = new Button("Insert");
		btnInsert.setText("เพิ่ม");
		btnInsert.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				addrDeptList.add(new ContDept());
				setFields();
			}
		});
		add(btnInsert, 23, 109);
		btnInsert.setSize("75px", "26px");

		addrDeptList = new ArrayList<ContDept>();
		addrDeptList.clear();
		itemData.setList(addrDeptList);
		itemData.refresh();
	}

	@Override
	protected void clearFields() {
		super.clearFields();
		addrDeptList.clear();
		itemData.setList(addrDeptList);
		itemData.refresh();
	}

	@Override
	protected void setFields() {
		super.setFields();
		itemData.setList(addrDeptList);
		itemData.refresh();
	}

	@Override
	protected void search() {

	}

	@Override
	protected void refresh() {
		Library.getSaleService()
				.selectAddrDept(txtCode.getText(), dataCallback);

	}

	@Override
	protected void update() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void insert() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void delete() {
	
	}

	@Override
	protected void getFields() {
		// TODO Auto-generated method stub

	}

	@Override
	protected String getReportFile() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected Map<String, Object> genReportParams() {
		// TODO Auto-generated method stub
		return null;
	}

	AsyncCallback<List<ContDept>> dataCallback = new AsyncCallback<List<ContDept>>() {

		@Override
		public void onSuccess(List<ContDept> result) {
			addrDeptList.clear();
			addrDeptList.addAll(result);
			setFields();
		}

		@Override
		public void onFailure(Throwable caught) {
			Window.alert(Library.ERROR_MESSAGE);
		}
	};
}
